package chapter1;

import edu.princeton.cs.algs4.StdRandom;

import java.util.Arrays;
import java.util.Random;

/**
 * 一堆数中，查找两个数相加=0的数对的个数
 */
public class TwoSum {


    public static int count(int[] array){

        Arrays.sort(array);

        System.out.println(Arrays.toString(array));

        int count = 0;
        for (int i = 0; i < array.length; i++) {
            int j = Arrays.binarySearch(array, i, array.length, -array[i]);
            if (j > i){
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {

        Random random = new Random();

        int[] arr = new int[10000];
        for (int i = 0; i < 10000; i++) {
            arr[i] = random.nextInt(20000+1) - 10000;
        }

        System.out.println(count(arr));
    }

}
